//
//  LLTabbarViewController.m
//  LLLLL
//
//  Created by mc on 2019/03/18.
//  Copyright © 2019年 admin. All rights reserved.
//

#import "LLTabbarViewController.h"
#import "LLNavigationViewController.h"
#import "TTHomeViewController.h"
#import "TTMessageViewController.h"
#import "TTShopCartViewController.h"
#import "TTMineViewController.h"
#import "TTSearchViewController.h"
#import "TTTotalOrderViewController.h"
#import "TTRecyclingHomeViewController.h"
#import "TTRecyclingNavigationViewController.h"

@interface LLTabbarViewController ()<UITabBarControllerDelegate>

@property (nonatomic, assign) NSInteger lastIndex;  // 上次选中的

@end

@implementation LLTabbarViewController

#pragma mark - 生命周期
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加子控制器
    [self createViewControllers];
    
    // 选中的字体颜色
    self.tabBar.tintColor = kMainColor;
    
//    // 生成一张图片
    UIColor *color = [[UIColor whiteColor] colorWithAlphaComponent:1];
    UIImage *image = [UIImage imageWithColor:color];
    //想要隐藏这条线，必须要设置tabBar.backgroundImage
    self.tabBar.shadowImage = [UIImage new];
    self.tabBar.backgroundImage = image;
    
    self.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shopCartRefreshData) name:@"shopcartdeletedataAndRefreshData" object:nil];
    
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    self.lastIndex = tabBarController.selectedIndex;
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(nonnull UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 1) {
        if (![TTLoginSingle sharedSingle].isLogin) {
            [self presentLoginViewControllerWithCloseBlock:^{
//                [self setSelectedIndex:0];
                [LLRouteTool gotoHomeViewController];
            }];
            return;
        }
        TTRecyclingHomeViewController *recyclingVC = [[TTRecyclingHomeViewController alloc] init];
        TTRecyclingNavigationViewController *nav = [[TTRecyclingNavigationViewController alloc] initWithRootViewController:recyclingVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:nav animated:NO completion:nil];
        [tabBarController setSelectedIndex:self.lastIndex];
        
    } else if (tabBarController.selectedIndex == 2) {
        TTMessageViewController *messageVC = [[TTMessageViewController alloc] init];
        messageVC.defaultSelectIndex = 1;
        messageVC.isPresent = YES;
        LLNavigationViewController *nav = [[LLNavigationViewController alloc] initWithRootViewController:messageVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:nav animated:NO completion:nil];
        [tabBarController setSelectedIndex:self.lastIndex];
    } else if (tabBarController.selectedIndex == 3) {
        if (![TTLoginSingle sharedSingle].isLogin) {
            [self presentLoginViewControllerWithCloseBlock:^{
//                [self setSelectedIndex:0];
                [LLRouteTool gotoHomeViewController];
            }];
        }
    }
    
    CATransition *transition = [CATransition animation];
    transition.type = kCATransitionFade;
    [self.tabBar.layer addAnimation:transition forKey:nil];
    
}

// 不是在tabbar中的购物车点击删除之后也要刷新tabbar中的购物车
- (void)shopCartRefreshData
{
//    LLNavigationViewController *nav = self.viewControllers[3];
//    TTShopCartViewController *shopCartVC = (TTShopCartViewController *)nav.viewControllers.firstObject;
//    [shopCartVC getShopCartData];
    
    LLNavigationViewController *nav = self.viewControllers[3];
    TTTotalOrderViewController *shopCartVC = (TTTotalOrderViewController *)nav.viewControllers.firstObject;
    NSDictionary *dic = shopCartVC.listContainerView.validListDict;
    NSNumber *key = [TTLoginSingle sharedSingle].userModel.VipLevel == 1 ? @1 : @0;
    UIViewController *vc = dic[key];
    if ([vc respondsToSelector:@selector(getShopCartData)]) {
        [vc performSelector:@selector(getShopCartData)];
    }
}



- (void)createViewControllers
{
    LLNavigationViewController *homeNav = [self createChildViewController:[TTHomeViewController new] title:@"首页" tabbarImageName:@"home" selectedImageName:@"home_sel"];
    TTRecyclingNavigationViewController *msgNav = [self createRecyclingChildViewController:[UIViewController new] title:@"回收" tabbarImageName:@"recycle" selectedImageName:@"recycle_sel"];
//    LLNavigationViewController *cartNav = [self createChildViewController:[TTShopCartViewController new] title:@"购物车" tabbarImageName:@"shopcart" selectedImageName:@"shopcart_sel"];
    LLNavigationViewController *orderNav = [self createChildViewController:[TTTotalOrderViewController new] title:@"订单" tabbarImageName:@"tab_order" selectedImageName:@"tab_order_sel"];
    
    UIViewController *searchVC = [UIViewController new];
    searchVC.tabBarItem.imageInsets = UIEdgeInsetsMake(-10, 0, 10, 0);
    LLNavigationViewController *searchNav = [self createChildViewController:searchVC title:@"搜索" tabbarImageName:@"search" selectedImageName:@"search"];
    LLNavigationViewController *mineNav = [self createChildViewController:[TTMineViewController new] title:@"我的" tabbarImageName:@"mine" selectedImageName:@"mine_sel"];
    
    self.viewControllers = @[homeNav, msgNav, searchNav, orderNav, mineNav];

}


- (LLNavigationViewController *)createChildViewController:(UIViewController *)childController title:(NSString *)title tabbarImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    childController.tabBarItem.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    LLNavigationViewController *nav = [[LLNavigationViewController alloc] initWithRootViewController:childController];
    
    return nav;
}

- (TTRecyclingNavigationViewController *)createRecyclingChildViewController:(UIViewController *)childController title:(NSString *)title tabbarImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    childController.tabBarItem.title = title;
    childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    TTRecyclingNavigationViewController *nav = [[TTRecyclingNavigationViewController alloc] initWithRootViewController:childController];
    
    return nav;
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
